//
//  GWTableViewController.m
//  壳牌喜力促销积分商城
//
//  Created by xrd on 15/3/15.
//  Copyright (c) 2015年 xrd. All rights reserved.
//

#import "GWTableViewController.h"
#import "GWTableViewCell.h"
//#import "DuiHuanViewController.h"
#import "ShopCarTool.h"
#import "UIView+JJ.h"
#import "GoodsModel.h"
@interface GWTableViewController ()
@property(nonatomic,strong)NSMutableArray *goodses;

@property(nonatomic,strong)UIView *bottomView;
@property(nonatomic,strong)UIButton *selectAll;
@property(nonatomic,strong)UILabel *scoresTip;
@end

@implementation GWTableViewController

//懒加载

-(NSMutableArray *)goodses{
    if (_goodses == nil) {
        // 加载数据

        _goodses = [NSMutableArray arrayWithArray:[ShopCarTool shopCarArray]];
    }
    return _goodses;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableFooterView = [[UIView alloc]init];
    
   self.tableView.rowHeight = 95;
    
    self.title = @"购物车";
 
//    NSLog(@"%@",self.superclass);
    
//   [self.superclass addSubview:_bottomView];
    
    
//    _bottomView .frame = CGRectMake( 0, -80, _bottomView.x, _bottomView.y);
    
//    [self.tabBarController.tabBar addSubview:_bottomView];
    
    
    
    

//    删除行的操作，需要启动表格的编辑模式
//    [self.tableView setEditing:YES];
    
    
    
}
#pragma mark - 添加底部工具条
- (void)setupBottomView

{
    
    _bottomView = [[UIView alloc]init];
    _bottomView.width = [UIScreen mainScreen].bounds.size.width;
    _bottomView.height = [UIScreen mainScreen].bounds.size.height * 0.1;
    _bottomView.y = [UIScreen mainScreen].bounds.size.height - _bottomView.height - 49;
    [_bottomView setBackgroundColor:[UIColor grayColor]];
    //    [_bottomView setAlpha:0.6];
    //    _bottomView .frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - _bottomView.height - 50, [UIScreen mainScreen].bounds.size.width , [UIScreen mainScreen].bounds.size.height * 0.1);
    
    
    
    // 添加到window上
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:_bottomView];
    //
    //全选
    
    UIButton *selectAll = [[UIButton alloc]init];
    
    [selectAll setBackgroundImage:[UIImage imageNamed:@"shop_noSelect"] forState:UIControlStateNormal];
    [selectAll setBackgroundImage:[UIImage imageNamed:@"shop_select"] forState:UIControlStateSelected];
    //    selectAll.size = selectAll.currentImage.size;
    selectAll.size = CGSizeMake(20, 20);
    //    selectAll.centerX = 30;
    //    selectAll.centerY = _bottomView.centerY;
    selectAll.y = 20;
    selectAll.x =20;
    
    
    NSLog(@"按钮大小%@",NSStringFromCGSize(selectAll.size));
    [selectAll addTarget:self action:@selector(selectAllClick:) forControlEvents:UIControlEventTouchUpInside];
    _selectAll = selectAll;
    [_bottomView addSubview:selectAll];
    
    //
    //    UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
    //    [_bottomView addSubview:btn];
    //积分计数
    UILabel *scoresTip = [[UILabel alloc]init];
    //    scoresTip.x = selectAll.x + selectAll.width + 30;
    scoresTip.x  = CGRectGetMaxX(selectAll.frame) +30;
    scoresTip.y = 10;
    scoresTip.width = 120;
    scoresTip.height = _bottomView.height -20;
    _scoresTip = scoresTip;
    _scoresTip.text =   [NSString stringWithFormat:@"需要积分:300"];
    [_scoresTip setTextColor:[UIColor redColor]];
    
    [_bottomView addSubview:_scoresTip];
    
    //兑换积分
    UIButton *exchangeBtn = [[UIButton alloc]init];
    [exchangeBtn setBackgroundImage:[UIImage imageNamed:@"shop_jieSuan"] forState:UIControlStateNormal];
    [exchangeBtn setTitle:@"兑换" forState:UIControlStateNormal];
    //    exchangeBtn.size = exchangeBtn.currentImage.size;
    exchangeBtn.size = CGSizeMake(80, 30);
    exchangeBtn.x = [UIScreen mainScreen].bounds.size.width - exchangeBtn.width - 10;
    exchangeBtn.y = 10;
    [exchangeBtn addTarget:self action:@selector(exchangeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:exchangeBtn];
    



}

//@selector(exchangeBtnClick)  按钮的点击事件     兑换
-(void)exchangeBtnClick{
    //1.点击兑换后，积分要作相应的减除
    
    
   //2.跳转到 货物地址填写的页面
}



//全选
-(void)selectAllClick:(UIButton*)sender{
    sender.selected = !sender.selected;
    _selectAll.selected = sender.selected;
    //全部选中给数组选中状态赋值
    for (GoodsModel *model in _goodses) {
        model.selected = _selectAll.selected;
        //重新刷新表格
        [self.tableView reloadData];
    }
    //计算积分
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.goodses.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    GWTableViewCell *cell = [GWTableViewCell tableViewCellWithTabelView:tableView];
//    NSInteger count = indexPath.row % 4 + 1;
//    NSString *nameImage = [NSString stringWithFormat:@"feilei%zd.png", count];
//    cell.imageView.image = [UIImage imageNamed:nameImage];
    
    
    static NSString *ID = @"GWCell";
    GWTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    //      GWTableViewCell *cell = [tableView
    
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"GWTableViewCell"owner:nil options:nil]lastObject];
    }
    //设置选中时背景不发生变化
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.goods = self.goodses[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //选中一行也执行check按钮 还是要用kvo或代理监听cell里面check按钮的变化，临时这样做
    GoodsModel *model = [[GoodsModel alloc]init];
    model = _goodses[indexPath.row];
    model.selected = YES;
    [self.tableView reloadData];
    /////
//    DuiHuanViewController *duihuaVc = [[DuiHuanViewController alloc] init];
//    duihuaVc.title = @"确认订单";
//    [self.navigationController pushViewController:duihuaVc animated:YES];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
//    NSInteger row = [indexPath row];
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [_goodses removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}

//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}



//底部工具条的设置

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = NO;
    [self setupBottomView];
    
}


// 控制的View将要消息前会调用此方法
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [_bottomView removeFromSuperview];
    _bottomView = nil;
}

@end
